<template>
  <div class="Home">
    <el-button style="margin: 12px 0;" type="primary" @click="addData">添加员工信息</el-button>
    <el-table :data="tableData" stripe border :header-cell-style="{background:'#FAFAFA'}">
      <el-table-column prop="EMPNO" label="员工编号"> </el-table-column>
      <el-table-column prop="ENAME" label="员工名称"> </el-table-column>
      <el-table-column prop="JOB" label="工作"> </el-table-column>
      <el-table-column prop="MGR" label="直属领导编号" width="120"> </el-table-column>
      <el-table-column label="入职时间" width="100">
        <template slot-scope="scope">
          {{scope.row.HIREDATE | filterData }}
        </template>
      </el-table-column>
      <el-table-column prop="SAL" label="工资"> </el-table-column>
      <el-table-column prop="COMM" label="奖金"> </el-table-column>
      <el-table-column prop="DEPTNO" label="部门编号"> </el-table-column>
      <el-table-column label="操作" width="90">
        <template slot-scope="scope">
          <el-button @click="updata(scope.row)" type="text" size="small" style="margin-right: 10px;">编辑</el-button>
          <el-popconfirm title="确定要删除吗？" @confirm="deldata(scope.row.EMPNO)">
            <el-button type="text" size="small" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑 -->
    <el-dialog :title="title==='add'?'添加员工信息':'编辑员工信息'" :visible.sync="show" width="500" :before-close="cancel">
      <el-form :model="form">
        <el-form-item label="员工编号" :label-width="formLabelWidth">
          <el-input style="width: 220px;" v-model="form.EMPNO" autocomplete="off" :disabled="title!=='add'"></el-input>
        </el-form-item>
        <el-form-item label="员工名称" :label-width="formLabelWidth">
          <el-input style="width: 220px;" v-model="form.ENAME" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="工作" :label-width="formLabelWidth">
          <el-input style="width: 220px;" v-model="form.JOB" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="直属领导编号" :label-width="formLabelWidth">
          <el-input style="width: 220px;" v-model="form.MGR" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" :label-width="formLabelWidth">
          <el-date-picker v-model="form.HIREDATE" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="工资" :label-width="formLabelWidth">
          <el-input style="width: 220px;" v-model="form.SAL" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="奖金" :label-width="formLabelWidth">
          <el-input style="width: 220px;" v-model="form.COMM" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门编号" :label-width="formLabelWidth">
          <el-input style="width: 220px;" v-model="form.DEPTNO" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
import {axios_getdata,axios_createData,axios_delData,axios_updata} from '../api/data_axios.js'
export default {
  name: "Home",
  data() {
    return {
      tableData: [],
      show:false,
      title:'',
      form: {
        EMPNO: '',
        ENAME: "",
        JOB: "",
        MGR: '',
        HIREDATE: "",
        SAL: '',
        COMM: '',
        DEPTNO: '',
      },
      formLabelWidth: '120px'
    };
  },
  mounted(){
    this.getData();
  },
  methods:{
    // 查询
    getData(){
      axios_getdata().then(res=>{ 
        this.tableData = res;
      })
    },
    // 取消
    cancel(){
      this.show = false;
      this.reset();
    },
    // 确定
    confirm(){
      this.show = false;
      // 新增
      if (this.title === 'add') {
        let data = this.form;
        axios_createData(data).then(res=>{ 
          if (res.code==200) {
            this.$message({
              showClose: true,
              message: '添加成功',
              type: 'success'
            });
            this.getData();
            this.reset();
          }else{
            this.$message({
              showClose: true,
              message: '添加失败',
              type: 'error'
            });
            this.reset();
          }
        })
      } else { // 编辑
        let data = this.form;
        axios_updata(data).then(res=>{
          if (res.code==200) {
            this.$message({
              showClose: true,
              message: '修改成功',
              type: 'success'
            });
            this.getData();
            this.reset();
          }else{
            this.$message({
              showClose: true,
              message: '修改失败',
              type: 'error'
            });
            this.reset();
          }
        })
      }
    },
    // 新增
    addData(){
      this.show = true;
      this.title = 'add';
    },
    // 编辑
    updata(item){
      this.show = true;
      this.title = 'updata';
      this.form = {...item}
      let date = new Date(this.form.HIREDATE);
      date.toLocaleDateString();
      this.form.HIREDATE = this.$moment(date).format("YYYY-MM-DD");
    },
    // 删除
    deldata(EMPNO){
      axios_delData(EMPNO).then(res=>{
        if (res.code==200) {
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          });
          this.getData();
        }
      })
    },
    // 重置数据
    reset(){
      for (const key in this.form) {
        this.form[key]= '';
      }
    }
  },
  filters:{
    filterData(val){
      let date = new Date(val);
      return date.toLocaleDateString();
    }
  }
};
</script>

<style lang="scss" scoped>
.Home{
  width: 800px;
  ::v-deep .el-dialog{
    margin-top: 2vh !important;
    width: 500px;
  }
}
</style>
